package com.yycx.module.bus.provider.strategy;

import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.module.bus.client.entity.BusSaleman;
import com.yycx.module.bus.client.entity.hy.HyOrder;
import com.yycx.module.bus.client.entity.hy.HyOrderDetail;
import com.yycx.module.user.client.entity.AppPosition;
import com.yycx.module.user.provider.service.AppPositionService;
import org.flowable.spring.boot.app.App;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 根据职务编号获取具体结算政策
 *
 * @author zyf
 */
@Service
public class SettleStrategyContext {
    private final Map<String, SettleStrategy> strategyServiceMap = new ConcurrentHashMap<>();
    private final AppPositionService appPositionService;

    public SettleStrategyContext(Map<String, SettleStrategy> strategyMap, AppPositionService appPositionService) {
        this.appPositionService = appPositionService;
        this.strategyServiceMap.clear();
        strategyMap.forEach((k, v) -> this.strategyServiceMap.put(k, v));
    }

    public SettleStrategy getSettleStrategy(String positionNo) {
        return strategyServiceMap.get("settleStrategy" + positionNo);
    }

    public void setSettleStrategy(AppPosition position, HyOrderDetail orderDetail, HyOrder order) {
        //渠道商
        Long saleManIdF = order.getSaleManIdF();
        if (FlymeUtils.isNotEmpty(saleManIdF)) {
            String positionNo = FlymeUtils.getString(position.getPositionNo(), "F");
            getSettleStrategy(positionNo).setSettlePolicy(orderDetail, order, saleManIdF);
        }
        //如果存在商务经理
        Long saleManIdD = order.getSaleManIdD();
        if (FlymeUtils.isNotEmpty(saleManIdD)) {
            getSettleStrategy("D").setSettlePolicy(orderDetail, order, saleManIdD);
        }
        //如果存在片区商务商
        Long saleManIdC = order.getSaleManIdC();
        if (FlymeUtils.isNotEmpty(saleManIdC)) {
            getSettleStrategy("C").setSettlePolicy(orderDetail, order, saleManIdC);
        }
        //如果存在商务总负责人
        Long saleManIdE = order.getSaleManIdE();
        if (FlymeUtils.isNotEmpty(saleManIdE)) {
            getSettleStrategy("E").setSettlePolicy(orderDetail, order, saleManIdE);
        }
    }

    /**
     * 设置结算金额
     *
     * @param orderDetail
     * @param order
     * @param variable
     */
    public void setSettleAmount(AppPosition position, HyOrderDetail orderDetail, HyOrder order, Map<String, Object> variable) {
        //渠道商
        Long saleManIdF = order.getSaleManIdF();
        if (FlymeUtils.isNotEmpty(saleManIdF)) {
            String positionNo = FlymeUtils.getString(position.getPositionNo(), "F");
            getSettleStrategy(positionNo).setSettleAmount(orderDetail, order, variable);
        }
        //如果存在商务经理
        Long saleManIdD = order.getSaleManIdD();
        if (FlymeUtils.isNotEmpty(saleManIdD)) {
            getSettleStrategy("D").setSettleAmount(orderDetail, order, variable);
        }
        //如果存在片区商务商
        Long saleManIdC = order.getSaleManIdC();
        if (FlymeUtils.isNotEmpty(saleManIdC)) {
            getSettleStrategy("C").setSettleAmount(orderDetail, order, variable);
        }
        //如果存在商务总负责人
        Long saleManIdE = order.getSaleManIdE();
        //1代表是商务经理预留时赋的默认值(saleManIdE=1时在SettleStrategyD中计算)
        if (FlymeUtils.isNotEmpty(saleManIdE) && !saleManIdE.equals(1L)) {
            getSettleStrategy("E").setSettleAmount(orderDetail, order, variable);
        }
    }
}
